from pymysql import *
import time
import hashlib
import random
import sys

value_list = ["json", "xml", "html", "list", "int", "float", "string"]
request_list = {"file":["readonly", "readwrite", "append",  "delete"],
                "sql": ["append",  "delete", "update", "select"],
                "api": ["get", "post", "put", "delete"],
                "app": ["cmd", "pipeline", "memory", "kill"]}
customer_list = {"sql": 1, "file": 2, "api": 3, "app": 4}


def get_keys():
    ret_list = []
    i = 1
    for value in value_list:
        for key,customer in customer_list.items():
            for request in request_list[key]:
                item = {"value_type": value,
                        "request_type": request, "customer": customer, "id": i}
                ret_list.append(item)
                i+=1
    return ret_list


def main():
    # 创建Connection连接
    conn = connect(host='localhost', port=3306, database='blackfish',
                   user='root', password='', charset='utf8')
    # 获得Cursor对象
    cs1 = conn.cursor()
    # 执行insert语句，并返回受影响的行数：添加一条数据
    # 增加
    count = 0

    item_list = get_keys()

    print(item_list)
    for item in item_list:
        count += cs1.execute(
            'insert into request(id,value_type,request_type,customer) values({},"{}","{}",{})'.format(item["id"],item["value_type"], item["request_type"], item["customer"]))

    print("成功添加{}条数据".format(count))
    # 提交之前的操作，如果之前已经之执行过多次的execute，那么就都进行提交
    conn.commit()

    # 关闭Cursor对象
    cs1.close()
    # 关闭Connection对象
    conn.close()


if __name__ == "__main__":
    main()
